﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CF.Data.Entity.FrmMemberEntity;

namespace CF.Member.DAL.Badges
{
    /// <summary>
    /// 左右逢源徽章
    /// </summary>
    public class InviteFriendBadge : BaseBadge
    {
        public InviteFriendBadge() : base(30, 0, 1, 10) { }

        public override bool CheckTakeCondition(int uid, int bid)
        {
            bool result = false;
            if (base.CheckTakeCondition(uid, bid))
            {
                using (caifendbEntities context = new caifendbEntities())
                {
                    int ivCount = context.FM_FriendInvite.Count(c => c.InviteUID == uid);
                    if (ivCount > 0)
                    {
                        FM_BadgeConfig bdcfg = context.FM_BadgeConfig.FirstOrDefault(c => c.BID == bid);
                        if (bdcfg != null)
                        {
                            int count;
                            if (!int.TryParse(bdcfg.CheckParam, out count))
                            {
                                count = 10;
                            }
                            if (ivCount >= count)
                            {
                                result = true;
                            }
                        }
                    }
                }
            }
            return result;
        }

        public override UpdateBadgeStatus UpdateBadgeLevel(int bid, int uid)
        {
            UpdateBadgeStatus result = UpdateBadgeStatus.Default;
            using (caifendbEntities context = new caifendbEntities())
            {
                FM_MyBadges myBadge = context.FM_MyBadges.FirstOrDefault(c => c.BID == bid && c.UID == uid && c.Status == BadgeActState.Activation);
                if (myBadge != null)
                {
                    int inviteCount = myBadge.CurrParam ?? 0;
                    inviteCount++;
                    myBadge.CurrParam = inviteCount;
                    myBadge.LastUpdateTime = DateTime.Now;
                    byte blvl = myBadge.BadgeLevel ?? BaseBadgeLevel;
                    result = UpdateBadgeStatus.AddGrowUpPoint;
                    //判断是否能升级
                    if (blvl < MaxBadgeLevel)
                    {
                        int nextLvl = blvl + 1;
                        int updLvl = 6 * (nextLvl * nextLvl - 1);
                        if (myBadge.CurrParam >= updLvl)
                        {
                            blvl++;
                            myBadge.BadgeLevel = (byte)blvl;
                            int cents = myBadge.CurrAwardCents ?? BaseAwardCents;
                            cents += 30;
                            myBadge.CurrAwardExp = cents;
                            result = UpdateBadgeStatus.UpdateSuccess;
                        }
                    }
                    else
                    {
                        result = UpdateBadgeStatus.FullLevel;
                    }
                }
                else
                {
                    result = UpdateBadgeStatus.NotFound;
                }
            }
            return result;
        }
    }
}
